//
//  JLSelectCityTableViewController.m
//  JLStyleApp
//
//  Created by LvJianfeng on 15/8/21.
//  Copyright (c) 2015年 JLStyle. All rights reserved.
//

#import "JLSelectCityTableViewController.h"
#import "JLCommon.h"
@interface JLSelectCityTableViewController()
@property (strong, nonatomic) NSArray *data;
@end

@implementation JLSelectCityTableViewController
- (void)initData{
    JLCommon *shareManager = [JLCommon shareManager];
    self.data = [shareManager get_CityByProvinces:_pid];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    EmtryCellHidden(self.tableView);
    [self initData];
    self.title = @"选择城市";
}

- (void)cancelAction{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *Identifier = @"CityCell";
    JLSelectCityCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
    if (cell==nil) {
        cell = [[JLSelectCityCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
    }
    City *city = self.data[indexPath.row];
    cell.cityNameLabel.text = city.name;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    City *city = self.data[indexPath.row];
    [self.delegate chooseCityAction:city];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark lazyload
- (NSArray *)data{
    if (_data==nil) {
        _data = [[NSArray alloc] init];
    }
    return _data;
}

@end

@implementation JLSelectCityCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //self.selectionStyle     = UITableViewCellSelectionStyleNone; //点击无背景
        self.backgroundColor    = [UIColor clearColor];
        UILabel *cityNameLabel  = [[UILabel alloc] init];
        cityNameLabel.frame         = CGRectMake(20, 50/2-20, ScreenWidth-35, 40);
        [self addSubview:cityNameLabel];
        self.cityNameLabel    = cityNameLabel;
    }
    return self;
}
@end